﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Hook.Environmental.Props;

namespace Hook.Environmental
{
    public class HingeChain
    {
        public const float SPRINGFACTOR = 5.5f;

        public List<HingeLink> HingeLinks;

        public HingeChain()
        {
            HingeLinks = new List<HingeLink>();
        }

        public void ReverseChainLinks()
        {
            HingeLinks.Reverse();

            foreach (HingeLink HL in HingeLinks)
            {
                Prop P = HL.Prop1;
                Vector3 V = HL.HingeOffset1;
                HL.Prop1 = HL.Prop2;
                HL.HingeOffset1 = HL.HingeOffset2;
                HL.Prop2 = P;
                HL.HingeOffset2 = V;
            }
        }

        public void SpringHingePositions()
        {
            Vector3 Distance, HingeLocation1, HingeLocation2;
            foreach (HingeLink HL in HingeLinks)
            {
                HingeLocation1 = Prop.RotateVectorAboutZ(HL.HingeOffset1, HL.Prop1.Rotation.Z);
                HingeLocation2 = Prop.RotateVectorAboutZ(HL.HingeOffset2, HL.Prop2.Rotation.Z);
                Distance = (HL.Prop2.Position + HingeLocation2) - (HL.Prop1.Position + HingeLocation1);
                Distance.Normalize();

                HL.Prop1.QueueForce(SPRINGFACTOR * HL.Prop1.Mass * Distance);
                HL.Prop2.QueueForce(SPRINGFACTOR * HL.Prop2.Mass * -Distance);
            }
        }
        public void SetHingePositions()
        {
            Vector3 Distance, HingeLocation1, HingeLocation2;
            foreach (HingeLink HL in HingeLinks)
            {
                HingeLocation1 = Prop.RotateVectorAboutZ(HL.HingeOffset1, HL.Prop1.Rotation.Z);
                HingeLocation2 = Prop.RotateVectorAboutZ(HL.HingeOffset2, HL.Prop2.Rotation.Z);
                Distance = (HL.Prop2.Position + HingeLocation2) - (HL.Prop1.Position + HingeLocation1);

                //Prop1.SetPosition(Prop1.Position + Distance / 2);
                HL.Prop2.SetPosition(HL.Prop2.Position - Distance);
            }
        }
    }
}
